# -*- coding: utf-8 -*-
"""


"""
import networkx as nx #导入建网络模型包，命名ne
import matplotlib.pyplot as plt #导入科学绘图包，命名 plt



'''
    1.According what we have learned about networkx,
     create four networks G1, G2, G3, and G4, 
     each representing the network in Fig 1-4 respectively.
     And then visualize them in networkx (label and figure title not needed).   
'''
G1 = nx.Graph()
G1.add_node("A") #添加一个节点 A
G1.add_node("B") #添加一个节点 B
G1.add_node("C") #添加一个节点 C
G1.add_node("D") #添加一个节点 D
G1.add_node("E") #添加一个节点 E
G1.add_edge("A","B") #添加一条边
G1.add_edge("B","C") #添加一条边
G1.add_edge("C","D") #添加一条边
G1.add_edge("C","D") #添加一条边
G1.add_edge("D","E") #添加一条边

nx.draw(G1)
plt.savefig("G1.png")           #输出方式1: 将图像存为一个png格式的图片文件
plt.show()      


G2 = nx.Graph()
G2.add_node("A") #添加一个节点 A
G2.add_node("B") #添加一个节点 B
G2.add_node("C") #添加一个节点 C
G2.add_node("D") #添加一个节点 D
G2.add_node("E") #添加一个节点 E
G2.add_node("F") #添加一个节点 F
G2.add_node("G") #添加一个节点 G

G2.add_edge("A","B") #添加一条边
G2.add_edge("B","C") #添加一条边
G2.add_edge("A","C") #添加一条边

G2.add_edge("C","D") #添加一条边
G2.add_edge("C","D") #添加一条边
G2.add_edge("D","E") #添加一条边

G2.add_edge("E","F") #添加一条边
G2.add_edge("E","G") #添加一条边
G2.add_edge("F","G") #添加一条边

nx.draw(G2)
plt.savefig("G2.png")           #输出方式1: 将图像存为一个png格式的图片文件
plt.show()   



G3 = nx.Graph()
G3.add_node("A") #添加一个节点 A
G3.add_node("B") #添加一个节点 B
G3.add_node("C") #添加一个节点 C
G3.add_node("D") #添加一个节点 D
G3.add_node("E") #添加一个节点 E
G3.add_node("F") #添加一个节点 F


G3.add_edge("A","B") #添加一条边
G3.add_edge("A","C") #添加一条边
G3.add_edge("A","D") #添加一条边
G3.add_edge("A","E") #添加一条边
G3.add_edge("A","F") #添加一条边

nx.draw(G3)
plt.savefig("G3.png")           #输出方式1: 将图像存为一个png格式的图片文件
plt.show()   



G4 = nx.Graph()
G4.add_node("A") #添加一个节点 A
G4.add_node("B") #添加一个节点 B
G4.add_node("C") #添加一个节点 C
G4.add_node("D") #添加一个节点 D
G4.add_node("E") #添加一个节点 E



G4.add_edge("A","B") #添加一条边
G4.add_edge("B","C") #添加一条边
G4.add_edge("B","D") #添加一条边
G4.add_edge("C","E") #添加一条边
G4.add_edge("D","E") #添加一条边

nx.draw(G4)
plt.savefig("G4.png")           #输出方式1: 将图像存为一个png格式的图片文件
plt.show()   


'''
2.We have also learned about basic Python programming and
 knowledge about random graph networks. You learned about the fact that 
 for a network to contain no isolated nodes, the p needs to be set 
 at 2*ln(n)/(n-1) because we derive the process mathematically 
 by solving a limit problem. Supposing that we know nothing about
 mathematics but still want to observe the process of decreasing 
 number of isolated nodes as p value increases. 
'''
def degree_percentage(n,p):
    G=nx.erdos_renyi_graph(10000,p)
    degree=nx.degree_histogram(G)
    return degree[0]

p = 1
xlist = []
ylist = []
plist = []
for i in range(1,10001):

    degree_0_percentage = degree_percentage(10000,p)/10000
    xlist.append(i)
    ylist.append(degree_0_percentage)
    plist.append(p)
    p = p +  0.0001
    print(i,p,degree_0_percentage)
    # if degree_0_percentage <= 0.000001:
    #     print("after %d repetition it take to reach a network with no isolated nodes" % i)
    #     break

        



# G=nx.erdos_renyi_graph(10000,0.0001)







# x=range(len(degree))#生成X轴序列，从1到最大度
# y=[z/float(sum(degree))for z in degree]#将频次转化为频率，利用列表内涵
# plt.loglog(x,y,color="blue",linewidth=2)#在双对坐标轴上绘制度分布曲线
# plt.show()#显示图表

# ps=nx.shell_layout(G_ER)#布置框架
# nx.draw(G_ER,ps,with_labels=False,node_size=30)
# plt.show()







